<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false" contentType="text/html; charset=UTF-8"%> 
<html>
<head>
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.6.2.js"></script>
</head> 
<body>
<h2>Hello World2!</h2>

<a href="<c:url value="/user/revisited"/>" id="test"/>ajax测试</a>
<div id="res"></div>
<form action="${pageContext.request.contextPath}/user/login" name="loginForm" id="loginForm" method="post">
<div style="">
	<div id="loginerror" style="font:red"></div>
	用户名：<input type="text" name="username" id="username"/><span id="nameerror" style="font:red"></span><br/>
	密&nbsp;&nbsp;码：<input type="password" name="password" id="password"/><span id="pwderror" style="font:red"></span><br>
	</br><input type="button" value="登录" id="loginButton" name="loginButton"/>
</div>
</form>
</body>
<script type="text/javascript">
	$("#test").click(function(){
		var link = $(this);
		alert(link);
		$.ajax({url:link.attr("href"),
			dataType:'text',
			success:function(text){
				$('#res').html(text);
			}
		}); 
		return false;
	});	
	$(function(){
		$("#loginButton").click(function(){
			var name = $("#username").val();
			var pwd = $("#password").val();
			if($.trim(name)==''){
				$('#nameerror').html('用户名不能为空');
				return;
			}
			if($.trim(pwd)==''){
				$('#pwderror').html('密码不能为空');
				return;
			}
			var flag = true;
			$.ajax({
				type:'post',
				async:false,
				url:"${pageContext.request.contextPath}/user/validateUser",
				data:'username='+name+'&password='+pwd,
				beforeSend: function(XMLHttpRequest){  
				   XMLHttpRequest.setRequestHeader("Accept", "text/plain");  
				},
				success:function(txt){
					if("TRUE"!=txt){
						$('#loginerror').html(txt);
						flag = false;
					}
					return;
				}	
			});
			if(flag){
				$('#loginForm').submit();
			}
		});
		
	});
	
</script>
</html>
